package com.tanhua.dubbo.api;




import com.tan.model.domain.UserInfo;
import com.tan.model.mongo.RecommendUser;
import com.tan.model.mongo.UserLike;
import com.tan.model.vo.PageResult;
import com.tan.model.vo.TodayBest;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @DubboReference
    private UserInfoapi userInfoApi;
    //查询今日佳人
    @Override
    public RecommendUser queryWithMaxScore(Long touserId) {
        Criteria criteria = Criteria.where("toUserId").is(touserId);
        Query query=Query.query(criteria).with(Sort.by(Sort.Order.desc("score"))).limit(1);
        RecommendUser recommendUser = this.mongoTemplate.findOne(query, RecommendUser.class);

        return recommendUser;
    }

    @Override
    public TodayBest queryWithtodaybest(Long id) {
        RecommendUser recommendUser = this.queryWithMaxScore(id);
        if(recommendUser == null){
            recommendUser = new RecommendUser();
            recommendUser.setUserId(1L);
            recommendUser.setScore(99d);
        }
       Long userId = recommendUser.getUserId();
     UserInfo userInfo =    userInfoApi.getUserInfo(userId);
     TodayBest todayBest = TodayBest.init(userInfo, recommendUser);


        return todayBest;
    }
    public PageResult queryRecommendUserList(Integer page, Integer pagesize, Long toUserId) {
        //1、构建Criteria对象
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //2、创建Query对象
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score"))).limit(pagesize)
                .skip((page - 1) * pagesize);
        //3、调用mongoTemplate查询
        List<RecommendUser> list = mongoTemplate.find(query, RecommendUser.class);
        long count = mongoTemplate.count(query, RecommendUser.class);
        //4、构建返回值PageResult
        return  new PageResult(page,pagesize, Math.toIntExact(count),list);
    }

    @Override
    public RecommendUser querythisUser(Long id, Long userId) {
      Criteria criteria = Criteria.where("userId").is(id).and("toUserId").is(userId);
      Query query = Query.query(criteria);
     RecommendUser recommendUser = mongoTemplate.findOne(query,RecommendUser.class);
     return recommendUser;
    }

    @Override
    public List<RecommendUser> queryCardsList(Long id, int i) {
        Criteria criteria = Criteria.where("userId").is(id);
        List<UserLike> userLikes = mongoTemplate.find(Query.query(criteria), UserLike.class);
        List<Long> ids = new ArrayList<>();
        for (UserLike userLike : userLikes) {
            ids.add(userLike.getLikeUserId());
        }

        Criteria criteria1 = Criteria.where("userId").nin(ids).and("toUserId").is(id);
        Query query = Query.query(criteria1);
        List<RecommendUser> list = mongoTemplate.find(query, RecommendUser.class);
        Collections.shuffle(list);
        return list.subList(0, Math.min(i, list.size()));
    }



}
